<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-10-04
 * Time: 11:57
 */
namespace app\api\controller;

use app\common\controller\Apibase;
use app\common\model\Sms as cxModel;

class Sms extends Apibase{

    /**
     * 本方法接收GET或POST值
     * 查询字段为AID或UID
     * 或查询字段不存在，则查询所有评论
     * 返回json数组 包括加密后用用户名及用户昵称及用户头像
     * @return \think\response\Json
     */
    public function getList(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $cxmodel = new cxModel();
        $getlist = $cxmodel->getList($getdata);
        if($getlist == false){
            $res['msg'] = '暂无信息';
            return json($res);
        }
        $res['code'] = '1';
        $res['msg'] = '查询成功';
        $res['cont'] = $getlist;
        return json($res);
    }
    //  更改显示状态
    public function see(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        if(empty($getdata['id'])){
            $res['msg'] = '非法访问';
            return json($res);
        }
        $cxmodel = new cxModel();
        if(empty($getdata['a'])){
            $sms = $cxmodel->where('id',$getdata['id'])->find();
            if(empty($sms)){
                $res['msg'] = '信息不存在';
                return json($res);
            }
            $sms = $sms->toArray();
            if($sms['status'] == '0'){
                $cxmodel->where('id',$sms['id'])->update(['status' => '1']);
            }
        }
        if(!empty($getdata['a']) && $getdata['a'] == 'all'){
            if(empty($getdata['uid'])){
                $res['msg'] = '非法请求';
                return json($res);
            }
            $cxmodel->where('uid',$getdata['uid'])->update(['status' => '1']);
        }
        $sms['status'] = '1';
        $res['code'] = '1';
        $res['msg'] = '信息已读';
        $res['cont'] = $sms;
        return json($res);
    }
    //  删除站内信
    public function del(){
        $res['code'] = '0';
        if(!request()->isPost()){
            $res['msg'] = '非法访问';
            return json($res);
        }
        $getdata = input('post.');
        $getdata = datatrim($getdata);
        if(empty($getdata['id']) || empty($getdata['uid'])){
            $res['msg'] = '非法访问';
            return json($res);
        }
        $cxmodel = new cxModel();
        if(empty($getdata['a'])){
            $sms = $cxmodel->where('id',$getdata['id'])->find();
            if($getdata['uid'] != $sms['uid']){
                $res['msg'] = '非法访问';
                return json($res);
            }
            $cxmodel->where('id',$sms['id'])->delete();
        }
        if(!empty($getdata['a']) && $getdata['a'] == 'all'){
            if(empty($getdata['uid'])){
                $res['msg'] = '非法访问';
                return json($res);
            }
            $cxmodel->where('uid',$getdata['uid'])->delete();
        }
        $res['code'] = '1';
        $res['msg'] = '删除成功';
        return json($res);
    }
}